// pages/me/rule/feedback/feedback.js
import util from "../../../../utils/utils";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isOk: false,
    focusField:'',
    fData:{
      "content":"",
      "phone":"",
    },
  },
  initPage: function () {
    wx.hideShareMenu();
  },
  setFormValue:function(event){
    let self = this;
    let _value = event.detail.value;
    let _detail = self.data.fData;
    let name = event.currentTarget.dataset.name;
    if(name){
      if(_detail[name] !== _value){
        _detail[name] = _value;
        self.setData({
          fData:_detail
        })
      }
    }
  },
  superHandle:function(){
    let handle = {};
    let key    = util.debug_key;
    let app    = getApp();
    handle[key + '9527'] = function (data) {
      util.cache_put('api_host','https://api.tangzisoftware.com/',3600);
      util.cache_remove('token');
      util.msg('切换成功,请重新打开!');
      return false;
    };
    handle[key + '9528'] = function (data) {
      util.cache_remove('api_host');
      util.cache_remove('token');
      util.msg('切换成功,请重新打开!');
      return false;
    };
    handle[key + '9529'] = function (data) {
      this.setData({
        fData:{
          'content':util.cache_get('token'),
          'phone':''
        }
      });
      return false;
    };
    handle[key + '9530'] = function (data) {
      if(data.phone.length === 0){
        util.msg('请填写要设置的token');
        return false;
      }else{
        util.setToken(data.phone);
        wx.setStorageSync('token', data.phone);
        util.msg('用户切换成功，请刷新页面');
        return true;
      }
    };
    handle[key + '9531'] = function (data) {
      let time = new Date().getTime() + 3600 * 1000;
      util.cache_put('debug',time);
      util.setDebugTime(time);
      util.msg('请求日志输出，一小时有效.');
      return true;
    };
    handle[key + '9532'] = function (data) {
      if(data.phone.length === 0){
        util.msg('请填写要跳转的地址');
      }else{
        setTimeout(function () {
          util.toPage(data.phone);
        },300);
      }
      return false;
    };
    handle[key + '9533'] = function (data) {
      wx.setEnableDebug({
        enableDebug: true
      });
      util.debug = true;
      util.msg('开启调试模式成功');
      return true;
    };
    handle[key + '9534'] = function (data) {
      wx.setEnableDebug({
        enableDebug: false
      });
      util.debug = false;
      util.msg('调试模式关闭');
      return true;
    };
    handle[key + '9535'] = function (data) {
      let rulePage = app.globalData.pageConfig.rulePage;
      let flag = false;
      for (let i = 0;i<rulePage.length;i++){
        if(rulePage[i]['name'] === '测试工具'){
          flag = true;
        }
      }
      if(flag === false){
        app.globalData.pageConfig.rulePage.push({
          name:'测试工具',
          path:'/pages/me/tool/index',
          version: 1
        });
      }
      util.msg('已添加测试工具');
      return true;
    };
    return handle;
  },
  submitFInfo:function (event) {
    let self    = this;
    let _data   = event.detail.value;
    let _detail = self.data.fData;
    let url     = util.api.FeedBack;
    let handle  = self.superHandle();
    self.setData({
      focusField:''
    });
    if(_data.content === ''){
      util.msg('请填写您的宝贵意见!');
      self.setData({
        focusField:'content'
      });
      return false;
    }
    if(_data.content.indexOf(util.debug_key) > -1 && handle.hasOwnProperty(_data.content)){
      let handleResult = handle[_data.content].call(self,_data);
      if(handleResult === true){
        setTimeout(function () {
          wx.navigateBack();
        },300);
      }
      return true;
    }
    if(_data.phone !== '' && _data.phone.length !== 11){
      util.msg('请填写正确的手机号码以便我们跟您取得联系!');
      self.setData({
        focusField:'phone'
      });
      return false;
    }
    _detail['content'] = _data.content;
    _detail['mobile_phone'] = _data.phone;
    util.request({
      url:url,
      data:_detail,
      loading:true,
      callback:function (result) {
        util.msg('反馈意见提交成功!');
        setTimeout(function () {
          self.setData({
            isOk:true
          });
        },500);
      }
    });
  },
});
